package com.lagou.order.controller;

import com.github.pagehelper.PageInfo;
//import com.lagou.commen.feign.GoodsFeign;
import com.lagou.commen.pojo.Goods;
import com.lagou.commen.pojo.Order;
import com.lagou.commen.pojo.OrderVO;
import com.lagou.commen.pojo.ResponseResult;
import com.lagou.order.feign.GoodsFeign;
import com.lagou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/query/{id}")
    public Order findById(@PathVariable Integer id) {
        return orderService.findById(id);
    }


    @PostMapping("/selectByPage")
    public ResponseResult selectByPage(@RequestBody OrderVO orderVO) {
        PageInfo<Order> pageInfo = orderService.selectByPage(orderVO);
        //封装信息
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMessage("查询成功");
        responseResult.setState(200);
        responseResult.setSuccess(true);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        responseResult.setContent(map);
        return responseResult;
    }


    @Autowired
    private GoodsFeign goodsFeign;


    @GetMapping("/selectGoodsIdListByOrderId/{id}")
    public ResponseResult selectGoodsIdListByOrderId(@PathVariable Integer id) {
        List<Integer> goodsIdList = orderService.selectGoodsIdListByOrderId(id);
        List<Goods> goodsList = new ArrayList<>();
        for (Integer i : goodsIdList) {
            goodsList.add(goodsFeign.findById(i));
        }

        //封装数据
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMessage("查询成功");
        responseResult.setState(200);
        responseResult.setSuccess(true);
        responseResult.setContent(goodsList);
        return responseResult;
    }



























    /*@Autowired
    private RestTemplate restTemplate;

    //服务注册中心客户端对象
    @Autowired
    private DiscoveryClient discoveryClient;*/



   /* @GetMapping("/query/{id}")
    public Goods findDataById(@PathVariable Integer id) {
        return goodsFeign.findById(id);
       *//* List<ServiceInstance> instances = discoveryClient.getInstances("lagou-service-goods");
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        String url = "http://" + host + ":" + port + "/goods/query/" + id;
        return restTemplate.getForObject(url, Goods.class);*//*
    }*/

   /* @GetMapping("/getProductServerPort")
    public String getGoodsServerPort() {
        *//*String url = "http://lagou-service-product/service/port/";

        return restTemplate.getForObject(url, String.class);*//*

        return goodsFeign.getPort();

    }*/

    /*@HystrixCommand(
            threadPoolKey = "getProductServerPort2",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"), // 线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20") // 等待队列长
            },
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            }
    )
    @GetMapping("/getGoodsServerPort2")
    public String getGoodsServerPort2() {
        *//*String url = "http://lagou-service-product/service/port/";

        return restTemplate.getForObject(url, String.class);*//*
        return goodsFeign.getPort();
    }

    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    //统计窗口时间的设置
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计窗口内的最小请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计窗口内错误请求阈值的设置 50%
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //自我修复的活动窗口时间
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")

            },
            fallbackMethod = "myFallBack"
    )
    @GetMapping("/getGoodsServerPort3")
    public String getGoodsServerPort3() {
        *//*String url = "http://lagou-service-goods/service/port/";

        return restTemplate.getForObject(url, String.class);*//*
        return goodsFeign.getPort();
    }
*/
   /* public String myFallBack() {
        return "-1"; // 兜底数据
    }*/
}
